import com.github.pagehelper.PageInfo;
import service.UserService;
import util.Sql;
import pojo.User;
import mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.testng.annotations.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class tt {

    @Test
    public void testselectById() throws IOException{
        //接收参数
        int id=1;
        //1.加载核心配置文件
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession=sqlSessionFactory.openSession();

        UserMapper users=sqlSession.getMapper(UserMapper.class);

        User user=users.selectById(id);

        System.out.println(user);
        sqlSession.commit();
        //4释放资源
        sqlSession.close();
        }

    @Test
    public void selectLogin() throws IOException{
        //接收参数
        String usersname="123";
        String password="123";
        int id=2;
        //处理参数  模糊查询
//        name= "%"+ name +"%";

        //2封装对象
//        User user=new User();
//        user.setUsersname(usersname);
//        user.setPassword(password);

        //3map
        Map map=new HashMap();
        map.put("id",id);
//        map.put("password",password);
        //1.加载核心配置文件
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession=sqlSessionFactory.openSession();

        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);

        //1.User user=users.selectLogin(usersname,password);
        //2.User users=userMapper.selectLogin(user);
//        3.User users=userMapper.selectLogin(map);

        User users=userMapper.select(map);
        System.out.println(users);
        sqlSession.commit();
        //4释放资源
        sqlSession.close();
    }
    @Test
    public void add() throws IOException{
        //接收参数
        String usersname="29029";
        String password="29029";
        String name="气功";
        String work="电镀";
        String interest="飞升";
        String sex="未知";
        String remark="son";
        int id=0;
        //2封装对象
        User user=new User();
//        user.setId(id);
        user.setUsersname(usersname);
        user.setPassword(password);
        user.setName(name);
        user.setWork(work);
        user.setInterest(interest);
        user.setSex(sex);
        user.setRemark(remark);
        //1.加载核心配置文件
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession=sqlSessionFactory.openSession(true);//自动提交事物

        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);


        userMapper.insertUser(user);

//        sqlSession.commit();
        //4释放资源
        sqlSession.close();
    }
    @Test
    public void add2() throws IOException{
        //接收参数
        String usersname="chegua";
        String password="lw123";
        String name="气";
        String work="电";
        String interest="飞";
        String sex="男";
        String remark="son";
        //2封装对象
        User user=new User();
//        user.setId(id);
        user.setUsersname(usersname);
        user.setPassword(password);
        user.setName(name);
        user.setWork(work);
        user.setInterest(interest);
        user.setSex(sex);
        user.setRemark(remark);
        //1.加载核心配置文件
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession=sqlSessionFactory.openSession(true);//自动提交事物

        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);


        userMapper.insertUser(user);
        //主键返回<insert id="insertUser" useGeneratedKeys="true" keyProperty="id">
        Integer id=user.getId();
        System.out.println(id);
//        sqlSession.commit();
        //4释放资源
        sqlSession.close();
    }
    @Test
    public void testUpdate() throws IOException{
        //接收参数
        String usersname="666";
        String password="lw123";
        String name="妮露";
        String work="舞者";
        String interest="跳舞";
        String sex="女";
        String remark="花神之舞";
        int id=14;
        //2封装对象
        User user=new User();

        user.setUsersname(usersname);
//        user.setPassword(password);
//        user.setName(name);
//        user.setWork(work);
//        user.setInterest(interest);
//        user.setSex(sex);
//        user.setRemark(remark);
        user.setId(id);
        //1.加载核心配置文件
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession=sqlSessionFactory.openSession(true);//自动提交事物

        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);

        int count=userMapper.updateplus(user);

        System.out.println(count);
        //4释放资源
        sqlSession.close();
    }

    @Test
    public void testdele() throws IOException{
        //接收参数

        int id=11;

        //1.加载核心配置文件
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession=sqlSessionFactory.openSession(true);//自动提交事物

        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);

        userMapper.deleteByid(id);

        //4释放资源
        sqlSession.close();
    }
    @Test
    public void testdeles() throws IOException{
        //接收参数

        int[] ids={5,8,12};

        //1.加载核心配置文件
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession=sqlSessionFactory.openSession(true);//自动提交事物

        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);

        userMapper.deleteByids(ids);

        //4释放资源
        sqlSession.close();
    }
    @Test
    public  void login(){
        String usersname="13545";
        String password="135";
        SqlSession sqlSession= Sql.openSeeion();
        UserMapper users=sqlSession.getMapper(UserMapper.class);
        User user=new User();
        user.setUsersname(usersname);
        user.setPassword(password);
        User login=users.selectLogin(user);
        String name= login.getName();
        System.out.println(name);
        sqlSession.commit();
        //4释放资源
        sqlSession.close();
    }
    @Test
    public  void yanzhen(){
        String usersname="135";

        SqlSession sqlSession= Sql.openSeeion();
        UserMapper users=sqlSession.getMapper(UserMapper.class);

        sqlSession.commit();
        //4释放资源
        sqlSession.close();
    }

    @Test
    public  void off(){
        String usersname="135";

        SqlSession sqlSession = Sql.openSeeion();
        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);

        sqlSession.close();
    }
    @Test
    public static void main(String[] args) throws IOException {
        UserService service = new UserService();

       PageInfo<User> pageList=service.getPageList(1,2);
        System.out.println(pageList);
    }

}
